package com.china08.yxyapi.api.manage.stu.mystudy.huixue.scoreimprove;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;
import com.china08.yxyapi.service.mg.impl.YxyHuixuePlanKnowTrainQuesMistakeServiceImpl;

import io.swagger.annotations.ApiOperation;

/**
 * 成绩提升--计划详情--终止计划--确认
 */
@CrossOrigin
@RestController
public class YxyHuixuePlanStopController extends YxyApiBaseController {
    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;
    @Autowired
    private YxyHuixuePlanKnowTrainQuesMistakeServiceImpl yhpktqmsi;

    @ApiOperation(value = "成绩提升首页 终止计划")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/huixue/scoreimprove/stop", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        YxyHuixuePlan yxyHuixuePlan = yxyHuixuePlanRepository.findOne(reqModel.id);
        yxyHuixuePlan.setStatus(0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String endDate = sdf.format(new Date());
        yxyHuixuePlan.setEndDate(Integer.parseInt(endDate));
        if (yxyHuixuePlan.getProgress() < 100) {
            yxyHuixuePlan.setIconStatus(1);
            yxyHuixuePlan.setNotice("很遗憾，你没有完成既定的学习计划。不要气馁，重新开始吧！");
        } else {
            yxyHuixuePlan.setNotice("恭喜您，完成了本次学习计划！");
            yxyHuixuePlan.setIconStatus(2);
        }
        yxyHuixuePlanRepository.save(yxyHuixuePlan);
        RespModel respModel = new RespModel();
        YxyHuixuePlan yxyHuixuePlanupdate = yxyHuixuePlanRepository.findOne(reqModel.id);
        if (yxyHuixuePlanupdate != null && yxyHuixuePlanupdate.getStatus() == 0) {
            respModel.result = "终止计划成功";
            yhpktqmsi.saveMistakes(reqModel.id);
        } else {
            respModel.result = "终止计划失败";
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String id;// 计划id

    }

    protected static class RespModel extends YxyApiRespModel {

        public String result;// 修改结果

    }

}
